import sys
import time
import inspect
from selenium.common import TimeoutException, ElementClickInterceptedException
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from loguru import logger
from drivers.driver_manager import dm

# 最大递归深度，针对click_wait
# sys.setrecursionlimit(5)


class BasePage:

    def get(self, url):
        dm.dr.maximize_window()
        dm.dr.get(url)

    def move_mouse(self, loc, wait_tome):
        element = self.locator(loc, wait_tome)
        ActionChains(dm.dr).move_to_element(element).perform()

    def click(self, loc, wait_time=5):
        self.locator(loc, wait_time).click()

    def click_in_js(self, loc, loc_time=2):
        element = self.locator(loc, loc_time)
        dm.dr.execute_script("arguments[0].click();", element)

    def click_in_action_chains(self, loc, wait_time=3):
        el = self.locator(loc, wait_time)
        ActionChains(dm.dr).move_to_element(el).click().perform()

    def click_wait(self, loc, wait_time=3, deep=0):
        try:
            element = WebDriverWait(dm.dr, wait_time).until(
                EC.element_to_be_clickable(loc)
            )
            element.click()
        except TimeoutException:
            # 输出报错的路径
            temp = inspect.stack()[1:-1]
            caller = []
            for i in temp:
                caller.append(i.function)
            logger.warning(f'click_wait:  TimeoutException! :{caller}')
        except ElementClickInterceptedException as e:
            # 最大递归深度为5
            if deep >= 5:
                logger.error(f'click_wait:  点击超过五次！element: {str(loc)}')
                raise e
            else:
                logger.warning('click_wait:  元素被遮挡！ 尝试再点击一次')
                self.click_wait(loc, wait_time, deep+1)
        except Exception as e:
            logger.warning(f'click_wait:  发生了其他错误:{e}')

    def double_click(self, loc, wait_time=5):
        element = self.locator(loc, wait_time)
        ActionChains(dm.dr).double_click(element).perform()

    def input(self, loc, value):
        self.click_wait(loc)
        el = self.locator(loc)
        el.send_keys(value)

    def click_text_in(self, text, loc_input, loc_items):
        self.click(loc_input)
        loc_new = (loc_items[0], loc_items[1] + f'//*[contains(text(),"{text}")]')
        self.click(loc_new)

    def locator(self, loc, wait_time=2):
        dm.dr.implicitly_wait(wait_time)
        el = dm.dr.find_element(*loc)
        return el

    def locators(self, loc, wait_time=2):
        dm.dr.implicitly_wait(wait_time)
        return dm.dr.find_elements(*loc)

    def get_text(self, loc):
        return self.locator(loc).text

    def switch_to_window_by_title(self, title=None):
        if title:
            for handle in dm.dr.window_handles:
                dm.dr.switch_to.window(handle)
                if title in dm.dr.title:
                    break
        else:
            dm.dr.switch_to.new_window()


logger = logger
